# -*- coding: utf-8 -*-

# __date:       2021/6/28
# __author:     Yang Chao
# __function:   Longest Palindromic Substring

class Solution:

    def longestPalindrome(self, s: str) -> str:
        """
        暴力枚举：遍历每个子字符串，检查其是否为回文字符歘
        """

        # 检查字符串是否为回文字符串
        def cali_palin(s: str):
            left = 0
            right = len(s) - 1
            while(left < right):
                if s[left] != s[right]:
                    return False
                left += 1
                right -= 1
            return True

        left = 0
        right = 0
        res = 0
        for i in range(0, len(s)):
            for j in range(i + 1, len(s) + 1):
                if cali_palin(s[i:j]) and (j - i) > res:
                    left = i
                    right = j
                    res = j - i
        return s[left:right]

    def longestPalindrome2(self, s: str) -> str:
        """解法二：中心扩展算法"""
        ln = len(s)
        if ln < 2: return s
        res = ""
        def finder(l, r):
            nonlocal res
            while l >= 0 and r < ln and s[l] == s[r]:
                l -= 1
                r += 1
            res = s[l + 1:r] if (r - l - 1) > len(res) else res

        for i in range(ln):
            finder(i, i)
            finder(i, i + 1)
        return res
